package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"travel/project/appconfig"
	"travel/project/global"
	"travel/project/intdler/models"
)

var err error

func Inits() {

	mysqlName := appconfig.AppName.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/ooo?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlName.User,
		mysqlName.Password,
		mysqlName.Host,
		mysqlName.Port)
	global.MysqlDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
		return
	}
	fmt.Println("Mysql 连接成功")
	err = global.MysqlDB.AutoMigrate(&models.User{})
	if err != nil {
		panic(err)
		return
	}
	fmt.Println("迁移成功")
}
